package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult list(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(categoryPageQueryDTO.getName()), "name", categoryPageQueryDTO.getName())
                .eq(categoryPageQueryDTO.getType() != null, "type", categoryPageQueryDTO.getType())
                .orderByDesc("update_time");
        Page<Category> page = (Page<Category>) categoryMapper.selectList(queryWrapper);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.updateById(category);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
//        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<Category>();
//        updateWrapper.eq("id", id);
//        updateWrapper.set("status", status);
//        updateWrapper.set("updateUser", BaseContext.getCurrentId());
//        categoryMapper.update(updateWrapper);
        Category category = Category.builder().id(id).status(status).build();
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(Long id) {
        if(setmealMapper.exists(new QueryWrapper<Setmeal>().eq("category_id", id))) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        QueryWrapper<Dish> qw2 = new QueryWrapper<>();
        qw2.eq("category_id", id);
        if(dishMapper.exists(qw2)) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        categoryMapper.deleteById(id);
    }

    @Override
    public void insert(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.insert(category);
    }

    @Override
    public List<Category> selectByType(Integer type) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(type != null, "type", type);
        queryWrapper.orderByDesc("sort");
        return categoryMapper.selectList(queryWrapper);
    }
}
